package com.lc.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author liuc
 * @date 2021/8/16
 * @description
 **/
@Configuration
public class RabbitConfig {

    @Bean
    public Queue payQueue(){
        //待支付队列
        Map<String, Object> args = new HashMap<>();
        //消息过期时间60s
        args.put("x-message-ttl", 15*1000);
        //队列过期时间60s
        //args.put("x-expires",60*1000);
        //设置死信交换器
        args.put("x-dead-letter-exchange","ex.dlx");
        //设置死信交换器的路由key
        args.put("x-dead-letter-routing-key","key.dlx");
        return new Queue("queue.pay",true,false,false,args);
    }

    @Bean
    public Queue dlxQueue(){
        //====> 这里设置了一个空的map集合给arguments导致队列过期没有发送到私信队列
        return new Queue("queue.dlx",true,false,false,null);
    }

    @Bean
    public Exchange payExchange(){
        return new DirectExchange("ex.pay",true,false,null);
    }

    @Bean
    public Exchange dlxExchange(){
        return new DirectExchange("ex.dlx",true,false,null);
    }

    @Bean
    public Binding payBinding(){
        return new Binding("queue.pay",Binding.DestinationType.QUEUE,"ex.pay","key.pay",null);
    }

    @Bean
    public Binding dlxBinding(){
        return new Binding("queue.dlx",Binding.DestinationType.QUEUE,"ex.dlx","key.dlx",null);
    }

    @Bean("rabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return simpleRabbitListenerContainerFactory;
    }

}
